//creates image object
sCanvas.fn.image = function() {
	if (arguments.length !== 2 && arguments.length !== 3) {
		throw new Error("Wrong arguments was passed to image().");
	}
	
	try {
		var attrs,
			obj = new CanvasImage(this, arguments[0]);

		if (arguments.length === 2) {
			attrs = arguments[1];
		} else {
			attrs = {
				x: arguments[1],
				y: arguments[2]
			};
			obj.shorthand = true;
		}

		obj.attr(attrs);

		this.register(obj);
//		this.autorepaint();
		return obj;
	} catch (err) {
		throw(err);
	}
};



var CanvasImage = function(sc, source) {
	var that = this;
	this.init(sc);
	this.addSupportedAttr(["width", "height"/*, "wait"*/]);
	this.addAnimatableAttr(["width", "height"]);
	this.initAttrs();

	this.source = source;

	this.img = new Image();
	
//	if (source instanceof Image) {
//		this.img.src = source.src;
//	} else if (typeof source === "string") {
//		this.img.src = source;
//	} else {
//		throw new TypeError("Bad image source.");
//	}

	if (typeof source === "string") {
		this.img.src = source;
	} else if (source.tagName.toLowerCase() === "img") {
		this.img.src = source.src;
	} else {
		throw new TypeError("Bad image source.");
	}

	this.img.onload = function() {
		that.loaded = true;

		if (that.attrs.width === null) { //if width was not set to resize image
			that.attrs.width = this.width;
		}
		if (that.attrs.height === null) { //if height was not set to resize image
			that.attrs.height = this.height;
		}
		
		if (that.shorthand) {
			that.attrs.x += that.attrs.width / 2;
			that.attrs.y += that.attrs.height / 2;
			delete that.shorthand;
		}

		that.sc.autorepaint();
	};

	return this;
};

CanvasImage.prototype = new sCanvasObject();

CanvasImage.prototype.createFillPath = function(cx) {};

CanvasImage.prototype.createStrokePath = function(cx) {};

CanvasImage.prototype.createPointInPath = function(cx) {
	var center = this.center();
	
	cx.save();
	cx.translate(this.attrs.x, this.attrs.y);
	cx.rotate(this.attrs.rotation);
	cx.beginPath();
	cx.moveTo(
		-center.x - this.origin.x,
		-center.y - this.origin.y
	);
	cx.lineTo(
		-center.x - this.origin.x + this.attrs.width,
		-center.y - this.origin.y
	);
	cx.lineTo(
		-center.x - this.origin.x + this.attrs.width,
		-center.y - this.origin.y + this.attrs.height
	);
	cx.lineTo(
		-center.x - this.origin.x,
		-center.y - this.origin.y + this.attrs.height
	);
	cx.lineTo(
		-center.x - this.origin.x,
		-center.y - this.origin.y
	);
	cx.closePath();
	cx.restore();
};

CanvasImage.prototype.draw = function(cx) {
	var center;
	if (this.isVisible() && this.loaded) {
		center = this.center();
	
		//modificators ensures that image will not be blurred
		var modX = this.attrs.width % 2 === 1 ? 0.5 : 0;
		var modY = this.attrs.height % 2 === 1 ? 0.5 : 0;
		
		cx.shadowColor = this.attrs.shadowColor;
		cx.shadowBlur = this.attrs.shadowBlur;
		cx.shadowOffsetX = this.attrs.shadowX;
		cx.shadowOffsetY = this.attrs.shadowY;
		cx.globalAlpha = this.attrs.opacity;
		
		cx.save();
		cx.translate(this.attrs.x, this.attrs.y);
		cx.rotate(this.attrs.rotation);
		cx.drawImage(
			this.img,
			-center.x - this.origin.x + modX,
			-center.y - this.origin.y + modY,
			this.attrs.width,
			this.attrs.height
		);
		cx.restore();
	}
};

//returns center point as relative coordinates from base top left of object
CanvasImage.prototype.center = function() {
	return {
		x: this.attrs.width / 2,
		y: this.attrs.height / 2
	};
},
	
//stores passed image source
CanvasImage.prototype.source = null;

//stores HTMLImage object created from source
CanvasImage.prototype.img = null;

//returns true if image is loaded
CanvasImage.prototype.loaded = false;

CanvasImage.prototype.box = function() {
	var center, width, height, sinRot, sinRot2;
	if (this.attrs.rotation === 0) {
		center = this.center();
		return {
			top: this.attrs.y - center.y - this.origin.y,
			right: this.attrs.x - center.x - this.origin.x + this.attrs.width,
			bottom: this.attrs.y - center.y - this.origin.y + this.attrs.height,
			left: this.attrs.x - center.x - this.origin.x,
			width: this.attrs.width,
			height: this.attrs.height
		};
	} else {
		center = this.rotatedCenter();
		sinRot = Math.abs(Math.sin(this.attrs.rotation));
		sinRot2 = Math.abs(Math.cos(this.attrs.rotation));
		width = this.attrs.height * sinRot + this.attrs.width * sinRot2;
		height = this.attrs.width * sinRot + this.attrs.height * sinRot2;
		
		return {
			top: Math.round(center.y - height / 2),
			right: Math.round(center.x + width / 2),
			bottom: Math.round(center.y + height / 2),
			left: Math.round(center.x - width / 2),
			width: width,
			height: height
		};
	}
};